#include "music.hpp"
#include "color.h"
#include "../lvgl/lvgl.h"

LV_FONT_DECLARE(myFont);

// 进度条拖动后回调
void MusicUI::contro_cb(lv_event_t *e)
{
    int32_t playProcess = lv_bar_get_value(music_ui->contro);
    music_ctl->setPlayProcess(playProcess);
}

// 每秒调用一次的定时器, 用于更新进度条
void MusicUI::timer_cb(lv_timer_t *e)  /* static */
{
    auto playProcess = music_ctl->getPlayProcess();
    if (playProcess < music_ctl->getPlayingMusic().duration && !music_ctl->checkPaused()) {
        music_ctl->addPlayProcess();
        music_ui->setPlayProcess();
    } else if (playProcess >= music_ctl->getPlayingMusic().duration) {
        music_ctl->playNext();  // 播放下一首
    }
}

bool MusicUI::loadMusic()
{
    loadFiles();
    return music_ctl->size() > 0;
}

void MusicUI::addMusicToList()
{
    for (uint16_t i = 0; i < music_ctl->size(); i++) {
        addMusicItem(i);
    }
}

void MusicUI::addMusicItem(uint16_t index)
{
    music_t music = music_ctl->getMusic(index);
    uint16_t length = music.duration;

    auto *item = lv_btn_create(listCon);
    lv_obj_remove_style_all(item);
    lv_obj_set_style_pad_all(item, 10, 0);
    lv_obj_set_size(item, lv_pct(100), lv_pct(18));
    lv_obj_set_style_bg_opa(item, LV_OPA_COVER, 0);

    auto *txt = lv_label_create(item);
    lv_obj_set_style_text_font(txt, &myFont, 0);
    lv_label_set_text_fmt(txt, "%s\n%02d:%02d", music.fileName.c_str(), length / 60, length % 60);

    // 绑定点击事件: 播放另外一首歌
    lv_obj_add_event_cb(item, [](lv_event_t *e) {
        uint16_t index = lv_obj_get_index((lv_obj_t *)e->original_target);
        music_ctl->playOtherMusic(index); 
    }, LV_EVENT_CLICKED, nullptr);
}

void MusicUI::addMusicController(lv_obj_t *bar)
{
    // 显示正在播放的音乐的文件名
    musicFile = lv_label_create(bar);
    lv_obj_set_style_text_font(musicFile, &myFont, 0);
    lv_obj_align(musicFile, LV_ALIGN_TOP_LEFT, 20, 20);

    // 显示正在播放的音乐进度
    musicProcess = lv_label_create(bar);
    lv_obj_set_style_text_font(musicProcess, &myFont, 0);
    lv_obj_align(musicProcess, LV_ALIGN_TOP_RIGHT, -30, 20);

    // 进度条
    contro = lv_slider_create(bar);
    lv_obj_set_size(contro, lv_pct(100), 5);
    lv_obj_align(contro, LV_ALIGN_TOP_MID, 0, 8);
    lv_obj_add_event_cb(contro, contro_cb, LV_EVENT_VALUE_CHANGED, nullptr);

    // 定时器 每秒 更新进度条
    lv_timer_create(timer_cb, 1000, nullptr);

    // 播放控制框
    auto *optionCon = lv_obj_create(bar);
    lv_obj_remove_style_all(optionCon);
    lv_obj_set_size(optionCon, lv_pct(50), lv_pct(50));
    lv_obj_align(optionCon, LV_ALIGN_CENTER, -35, 12);

    // 播放选项 (随机或顺序)
    auto *optionBtn = lv_btn_create(optionCon);
    setStyle(optionBtn);
    static auto *optionTxt = lv_label_create(optionBtn);
    lv_label_set_text(optionTxt, music_ctl->checkRandom() ? LV_SYMBOL_SHUFFLE : LV_SYMBOL_LOOP);
    lv_obj_align(optionBtn, LV_ALIGN_LEFT_MID, 0, 0);
    lv_obj_add_event_cb(optionBtn, [](auto) {
        bool isRandom = music_ctl->toggleRandom();
        lv_label_set_text(optionTxt, isRandom ? LV_SYMBOL_SHUFFLE : LV_SYMBOL_LOOP);
    }, LV_EVENT_CLICKED, nullptr);

    // 播放/暂停按钮
    auto *playBtn = lv_btn_create(optionCon);
    setStyle(playBtn);
    static auto *playTxt = lv_label_create(playBtn);
    lv_label_set_text(playTxt, music_ctl->checkPaused() ? LV_SYMBOL_PLAY : LV_SYMBOL_PAUSE);
    lv_obj_align(playBtn, LV_ALIGN_CENTER, 0, 0);
    lv_obj_add_event_cb(playBtn, [](auto) {
        bool paused = music_ctl->togglePaused();
        playMusic();
        lv_label_set_text(playTxt, paused ? LV_SYMBOL_PLAY : LV_SYMBOL_PAUSE);
    }, LV_EVENT_CLICKED, nullptr);

    // 上一曲
    auto *prevBtn = lv_btn_create(optionCon);
    setStyle(prevBtn);
    auto *prevTxt = lv_label_create(prevBtn);
    lv_label_set_text(prevTxt, LV_SYMBOL_PREV);
    lv_obj_align_to(prevBtn, playBtn, LV_ALIGN_OUT_LEFT_MID, -10, 0);
    lv_obj_add_event_cb(prevBtn, [](auto) { music_ctl->playPrev(); }, LV_EVENT_CLICKED, nullptr);

    // 下一曲
    auto *nextBtn = lv_btn_create(optionCon);
    setStyle(nextBtn);
    auto *nextTxt = lv_label_create(nextBtn);
    lv_label_set_text(nextTxt, LV_SYMBOL_NEXT);
    lv_obj_align_to(nextBtn, playBtn, LV_ALIGN_OUT_RIGHT_MID, 10, 0);
    lv_obj_add_event_cb(nextBtn, [](auto) { music_ctl->playNext(); }, LV_EVENT_CLICKED, nullptr);

    // 音量调整弹框
    auto *volBtn = lv_btn_create(optionCon);
    setStyle(volBtn);
    auto *volTxt = lv_label_create(volBtn);
    lv_label_set_text(volTxt, LV_SYMBOL_VOLUME_MAX);
    lv_obj_align(volBtn, LV_ALIGN_RIGHT_MID, 0, 0);
    lv_obj_add_event_cb(volBtn, [](lv_event_t *e) {
        auto *mbox = lv_msgbox_create(nullptr);
        lv_obj_set_size(mbox, 300, 175);
        auto *closeBtn = lv_msgbox_add_close_button(mbox);
        music_ui->adjustVolume(mbox);
    }, LV_EVENT_CLICKED, nullptr);

    setUI();
}

void MusicUI::setPlayProcess()
{
    auto playProcess = music_ctl->getPlayProcess();
    auto duration = music_ctl->getPlayingMusic().duration;
    lv_slider_set_value(contro, playProcess, LV_ANIM_ON);
    lv_label_set_text_fmt(musicProcess, "%02d:%02d / %02d:%02d",
        playProcess / 60, playProcess % 60,
        duration / 60, duration % 60);
}

void MusicUI::setStyle(lv_obj_t *Btn)
{
    lv_obj_remove_style_all(Btn);
    lv_obj_set_style_pad_all(Btn, 20, 0);
}

void MusicUI::adjustVolume(lv_obj_t *parent)
{
    auto *con = lv_obj_create(parent);
    lv_obj_remove_style_all(con);
    lv_obj_set_size(con, lv_pct(100), lv_pct(60));
    lv_obj_set_flex_flow(con, LV_FLEX_FLOW_COLUMN);

    LV_IMAGE_DECLARE(Speaker);
    LV_IMAGE_DECLARE(Phone);

    volumnBar(con, Speaker, beep);
    volumnBar(con, Phone, phone);
}

void MusicUI::volumnBar(lv_obj_t *parent, const lv_image_dsc_t &img, AbstractVolume *vol)
{
    auto *bar = lv_obj_create(parent);
    lv_obj_remove_style_all(bar);
    lv_obj_set_size(bar, lv_pct(100), lv_pct(40));

    auto *image = lv_image_create(bar);
    auto *slider = lv_slider_create(bar);
    auto *value_label= lv_label_create(bar);

    lv_image_set_src(image, &img);
    lv_label_set_text_fmt(value_label, "%d", vol->getVolume());
    lv_slider_set_value(slider, vol->getVolume(), LV_ANIM_OFF);

    lv_obj_set_width(image, LV_PCT(25));
    lv_obj_set_width(slider, LV_PCT(55));
    lv_obj_set_width(value_label, LV_PCT(20));

    lv_obj_align(image, LV_ALIGN_LEFT_MID, 0, 0);
    lv_obj_align(slider, LV_ALIGN_CENTER, 0, 0);
    lv_obj_align(value_label, LV_ALIGN_RIGHT_MID, 10, 0);

    // 调整音量 vol 并同步 UI
    lv_obj_add_event_cb(slider, [](lv_event_t *e) {
        auto *slider = (lv_obj_t *)e->original_target;
        auto *value_label = lv_obj_get_child(lv_obj_get_parent(slider), 2);
        auto vol = lv_slider_get_value(slider);  // 获取滑块当前值

        ((AbstractVolume *)(e->user_data))->setVolume(vol);
        lv_label_set_text_fmt(value_label, "%d", vol);
    }, LV_EVENT_VALUE_CHANGED, vol);
}

void MusicUI::setUI()
{
    lv_slider_set_range(contro, 0, music_ctl->getPlayingMusic().duration);
    lv_label_set_text_fmt(musicFile, "%d--%s", music_ctl->getPlayingMusicIndex() + 1, music_ctl->getPlayingMusic().fileName.c_str());

    auto *playing = lv_obj_get_child(listCon, music_ctl->getPlayingMusicIndex());
    lv_obj_set_style_bg_color(playing, lv_color_hex(BEIGE), 0);
    setPlayProcess();
}

void MusicUI::resetUi() 
{
    auto *playing = lv_obj_get_child(listCon, music_ctl->getPlayingMusicIndex());
    lv_obj_set_style_bg_color(playing, lv_color_hex(WHITE), 0);
}

MusicUI::MusicUI()
{
    container = lv_obj_create(lv_scr_act());
    lv_obj_remove_style_all(container);
    lv_obj_set_size(container, lv_pct(85), lv_pct(100));
    lv_obj_align(container, LV_ALIGN_RIGHT_MID, 0, 0);
}

void MusicUI::freeMem()
{
    lv_free(container);
}

void MusicUI::build()
{
    if (loadMusic() == true) {
        // 音乐列表
        listCon = lv_obj_create(container);
        lv_obj_remove_style_all(listCon);
        lv_obj_set_size(listCon, lv_pct(100), lv_pct(80));
        lv_obj_set_flex_flow(listCon, LV_FLEX_FLOW_COLUMN);
        addMusicToList();

        // 音乐控制器
        auto *bar = lv_obj_create(container);
        lv_obj_remove_style_all(bar);
        lv_obj_set_size(bar, lv_pct(100), lv_pct(20));
        lv_obj_align(bar, LV_ALIGN_BOTTOM_MID, 0, 0);
        addMusicController(bar);

        music_ctl->loadSettings();
        startPlayMusic();
    } else {
        auto *label = lv_label_create(container);
        lv_obj_set_style_text_font(label, &myFont, 0);
        lv_label_set_text_fmt(label, "没有找到音乐文件");
        lv_obj_center(label);
    }
}
